using _ReplaceString_.UI.Styles;
using RSCore;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.ModLoader.UI;

namespace _ReplaceString_.UI.Elements;

public class LeftPanel : UIBase
{
	private UIBase _scroll;

	public LeftPanel(Vector2 size)
	{
		_scroll = new UIBase()
		{
			ExtraColor = Color.Gray,
			PaddingRight = 10,
		};
		_scroll.AddStyle(UIScroll.Vertical, UILayout.Vertical, UISize.Fill, UISize.FlexibleHeight);
		Append(_scroll);
		BackgroundColor = UICommon.DefaultUIBlue;
		Width.Pixels = size.X;
		Height.Pixels = size.Y;
		AddStyle(UIType.Panel, UIBackground.Rect, UIScroll.Window);
	}

	/// <summary>
	/// 显示文本需要尺寸数据，因此在OnInitialize里执行
	/// </summary>
	public override void OnInitialize() => ShowInfo(ModHelper.GetText("Config.Intro"));

	public void ShowInfo(string text)
	{
		_scroll.RemoveAllChildren();
		Recalculate();
		foreach (var line in Utils.WordwrapString(text,
			FontAssets.MouseText.Value,
			(int)_scroll._innerDimensions.Width,
			99,
			out _))
		{
			if (string.IsNullOrWhiteSpace(line))
			{
				continue;
			}

			var wrap = new UIBase();
			wrap.AddStyle(UISize.Fill, UISize.FixedHeight(36));
			_scroll.Append(wrap);

			var ui = new UIText(line)
			{
				Width = new(0, 1),
				Height = new(0, 1),
				TextOriginX = 0,
				TextOriginY = 0.5f,
			};
			wrap.Append(ui);
			wrap.Activate();
		}
		Recalculate();
	}

}